﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Interfaces;
using Microhard.FMCG.Buz.Models;
using HtmlAgilityPack;

namespace Microhard.FMCG.Buz.Impls.KASystem.WUMart
{
    public class ReceiveGoodsParser : IHtmlParser<List<OrderModel>>
    {
        public List<OrderModel> ParseObject(string html)
        {
            List<OrderModel> result = new List<OrderModel>();
            if (string.IsNullOrEmpty(html))
                return result;

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            //获得数据table
            var table = doc.GetElementbyId("MyDataGrid");
            if (table != null)
            {
                //获得tr
                var rows = table.Descendants("tr").Where(tr => tr.Attributes["align"].Value == "right").ToList();
                foreach (var tr in rows)
                {
                    var td = tr.Descendants("td").ToList(); ;
                    if (td.Count == 4)
                    {
                        OrderModel recOrder = new OrderModel();
                        recOrder.Code = td[0].InnerText;
                        recOrder.OrderStore = td[1].InnerText;
                        recOrder.OrderTypeName = td[2].InnerText;
                        recOrder.OrderDate = DateTime.Parse(td[3].InnerText);
                        result.Add(recOrder);
                    }
                }
            }
            return result;
        }
    }
}
